﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// ProgressLoading.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressLoading : UserControl
    {
        public ProgressLoading()
        {
            InitializeComponent();
            Loaded += ProgressLoading_Loaded;
        }

        private void ProgressLoading_Loaded(object sender, RoutedEventArgs e)
        {
            LineBuild();
            this.SetCurrentValue(StateProperty, LoadingState.Loading);
        }

        public double Step
        {
            get { return (double)GetValue(StepProperty); }
            set { SetValue(StepProperty, value); }
        }

        public static readonly DependencyProperty StepProperty =
            DependencyProperty.Register("Step", typeof(double), typeof(ProgressLoading), new PropertyMetadata(20.0, OnPropertyChanged));


        public Brush LineColor
        {
            get { return (Brush)GetValue(LineColorProperty); }
            set { SetValue(LineColorProperty, value); }
        }

        public static readonly DependencyProperty LineColorProperty =
            DependencyProperty.Register("LineColor", typeof(Brush), typeof(ProgressLoading), new PropertyMetadata(new SolidColorBrush(Colors.White), OnPropertyChanged));


        public LoadingState State
        {
            get { return (LoadingState)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State", typeof(LoadingState), typeof(ProgressLoading), new PropertyMetadata(LoadingState.UnLoading, OnStateChanged));


        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ProgressLoading)?.LineBuild();
        }

        private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LoadingState newValue = (LoadingState)e.NewValue;
            (d as ProgressLoading)?.UpdateState(newValue);
        }

        private void LineBuild()
        {
            if (Step <= 0)
                throw new Exception("步长不能小于等于 0");

            Cvs.Children.Clear();
            for (double x = 0; x < ActualWidth; x += Step)
            {
                Rectangle rect = new Rectangle()
                {
                    Width = 1,
                    Height = ActualHeight - 6,
                    Fill = LineColor
                };
                Canvas.SetLeft(rect, x);
                Canvas.SetTop(rect, 3);
                Cvs.Children.Add(rect);
            }

            for (double y = Step; y < ActualHeight; y += Step)
            {
                Rectangle rect = new Rectangle()
                {
                    Width = ActualWidth,
                    Height = 1,
                    Fill = LineColor
                };
                Canvas.SetTop(rect, y - 3);
                Cvs.Children.Add(rect);
            }
        }

        private void UpdateState(LoadingState state)
        {
            VisualStateManager.GoToState(this, state.ToString(), true);
        }
    }
}
